package com.lhf.springcloud.service.impl;
import java.util.Date;
import com.lhf.springcloud.DAO.StudentCompleteDataMapper;
import com.lhf.springcloud.DAO.StudentDao;
import com.lhf.springcloud.DAO.log.LogDataMapper;
import com.lhf.springcloud.Entity.StudentBasicInfo;
import com.lhf.springcloud.Entity.StudentCompleteData;
import com.lhf.springcloud.Entity.log.LogData;
import com.lhf.springcloud.service.StudentCompleteDataService;
import com.lhf.springcloud.utils.XmlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentCompleteDataImpl implements StudentCompleteDataService{
    @Autowired
    StudentCompleteDataMapper studentCompleteDataMapper;
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private LogDataMapper logDataMapper;
    @Override
    public List<StudentCompleteData> findstudentCompleteData(String idCard) {
        List<StudentCompleteData> studentCompleteData = studentCompleteDataMapper.findstudentCompleteData(idCard);
        return studentCompleteData;
    }

    @Override
    public Map findAllStudentBasicData(Integer startPage,Integer endPage,String idCard,String studentName) {
        Map map=new HashMap<>();
        List<StudentCompleteData> studentCompleteData = studentCompleteDataMapper.findAllStudentBasicData(startPage,endPage,idCard,studentName);
        int account = studentCompleteDataMapper.account();
        map.put("data",studentCompleteData);
        map.put("account",account);
        return map;
    }

    @Override
    public void insert(StudentCompleteData studentCompleteData) {
        Integer s=230215;
        String ss="510781199012";
        for (int i=1;i<20000;i++){
            s=s+i;
            String idCard=ss+s.toString();
            studentCompleteData.setIdCard(idCard);
            studentCompleteData.setStudentName("测试数据"+i);
            studentCompleteData.setSchoolstatus("在校");
            studentCompleteData.setSex("女");
            studentCompleteData.setStudentXJ("有学籍");
            studentCompleteData.setPolitical("团员");
            studentCompleteDataMapper.insert(studentCompleteData);
        }
    }

    @Override
    public String addStudentInfo(List<StudentCompleteData> list1) {
        /*在学生基础数据表里面查询身份证是否存在*/
        for (int i=0;i<list1.size();i++){
            List<StudentBasicInfo> byStudentIdCard = studentDao.findByStudentIdCard(list1.get(i).getIdCard());
            if (byStudentIdCard.size()==0){
                list1.get(i).setInfoJudge("false1");
                //return "学生基础信息中没有身份证为'"+course.get(i).getIdCard()+"',姓名为'"+course.get(i).getStudentName()+"',请检查该学生的身份证信息是否准确再上传数据";
            }
            if (byStudentIdCard.size()>1){
                return "联系管理员，基础数据身份证号码重复";
            }
            if (byStudentIdCard.size()==1){
                if (!byStudentIdCard.get(0).getStudentName().equals(list1.get(i).getStudentName())){
                    list1.get(i).setInfoJudge("false2");
                    //return "身份证为'"+course.get(i).getIdCard()+"',姓名为'"+course.get(i).getStudentName()+"',请检查该学生的姓名是否准确再上传数据";
                }
            }
        }


        for (int i=0;i<list1.size();i++){
            studentCompleteDataMapper.insert(list1.get(i));
        }
        return "完成操作";
    }

    @Override
    public void updateData(String oldValue,String dataValue,String id,String dataName) {
        LogData logData = new LogData();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        logData.setUpdateColumn(dataName);
        dataName = XmlUtils.getMapperPropertyByColumn("mapping/StudentCompleteDataMapping.xml", "BaseResultMap", dataName);//找到xml文件中对象属性对应的字段名
        studentCompleteDataMapper.updateData(dataValue,id,dataName);//修改的同时初始化infoJudge字段为null
        //将修改参数存入日志
        logData.setUpdateProperty(dataName);
        logData.setUpdateId(id);
        logData.setUpdateTable("StudentCompleteData");
        logData.setUpdateUser("admin");
        logData.setUpdateValue(dataValue);
        logData.setUpdateTime(df.format(new Date()));
        logData.setOldValue(oldValue);
        logDataMapper.addLogData(logData);
        //拿到修改对象的数据
        StudentCompleteData byId = studentCompleteDataMapper.findById(id);

        //判断基础数据中是否存在
        List<StudentBasicInfo> byStudentIdCard = studentDao.findByStudentIdCard(byId.getIdCard());
        if (byStudentIdCard.size()==0){
            studentCompleteDataMapper.updateStudentStatus("false1",id);
            //return "学生基础信息中没有身份证为'"+course.get(i).getIdCard()+"',姓名为'"+course.get(i).getStudentName()+"',请检查该学生的身份证信息是否准确再上传数据";
        }
        else if (byStudentIdCard.size()==1){
            if (!byStudentIdCard.get(0).getStudentName().equals(byId.getStudentName())){
                studentCompleteDataMapper.updateStudentStatus("false2",id);
                //return "身份证为'"+course.get(i).getIdCard()+"',姓名为'"+course.get(i).getStudentName()+"',请检查该学生的姓名是否准确再上传数据";
            }else {
                studentCompleteDataMapper.updateStudentStatus(null,id);
            }
        }
    }

    @Override
    public void updateAllStudentCompleteData(StudentCompleteData studentCompleteData, String id) {
        //拿到修改对象的数据
        StudentCompleteData byId = studentCompleteDataMapper.findById(studentCompleteData.getId());

        //判断基础数据中是否存在
        List<StudentBasicInfo> byStudentIdCard = studentDao.findByStudentIdCard(byId.getIdCard());
        if (byStudentIdCard.size()==0){
            studentCompleteData.setInfoJudge("false1");
            //studentCompleteDataMapper.updateStudentStatus("false1",id);
            //return "学生基础信息中没有身份证为'"+course.get(i).getIdCard()+"',姓名为'"+course.get(i).getStudentName()+"',请检查该学生的身份证信息是否准确再上传数据";
        }
        else if (byStudentIdCard.size()==1){
            if (!byStudentIdCard.get(0).getStudentName().equals(byId.getStudentName())){
                studentCompleteData.setInfoJudge("false2");
                //studentCompleteDataMapper.updateStudentStatus("false2",id);
                //return "身份证为'"+course.get(i).getIdCard()+"',姓名为'"+course.get(i).getStudentName()+"',请检查该学生的姓名是否准确再上传数据";
            }else {
                studentCompleteData.setInfoJudge(null);
                //studentCompleteDataMapper.updateStudentStatus(null,id);
            }
        }
        studentCompleteDataMapper.updateAllStudentCompleteData(studentCompleteData);
        LogData logData = new LogData();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        logData.setUpdateId(id);
        logData.setUpdateTable("学生基本信息");
        logData.setUpdateUser("admin");
        logData.setUpdateTime(df.format(new Date()));//为什么写在判断外面，写里面存在时间差，修改一条数据的时间我认为是一个时间点，计算机执行判断是要花时间的
        if (!byId.getBiogenicLand().equals(studentCompleteData.getBiogenicLand())){
            //将修改参数存入日志
        logData.setUpdateColumn("biogenicLand");
        logData.setUpdateProperty("biogenic_land");
        logData.setUpdateColumnName("生源地");
        logData.setUpdateValue(studentCompleteData.getBiogenicLand());
        logData.setOldValue(byId.getBiogenicLand());
        logDataMapper.addLogData(logData);
        }
        if (!byId.getBiogenicLandType().equals(studentCompleteData.getBiogenicLandType())){
            //将修改参数存入日志
            logData.setUpdateColumn("biogenicLandType");
            logData.setUpdateProperty("biogenic_land_type");
            logData.setUpdateColumnName("生源地类型");//判断字段如果被修改过就直接把修改的字段名字写死
            logData.setUpdateValue(studentCompleteData.getBiogenicLandType());//修改后的值存对象里
            logData.setOldValue(byId.getBiogenicLandType());//修改前的值存对象里
            logDataMapper.addLogData(logData);
        }
        if (!byId.getStuId().equals(studentCompleteData.getStuId())){
            //将修改参数存入日志
            logData.setUpdateColumn("stuId");
            logData.setUpdateProperty("stu_id");
            logData.setUpdateColumnName("学号");
            logData.setUpdateValue(studentCompleteData.getStuId());
            logData.setOldValue(byId.getStuId());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getSchoolstatus().equals(studentCompleteData.getSchoolstatus())){
            //将修改参数存入日志
            logData.setUpdateColumn("schoolstatus");
            logData.setUpdateProperty("schoolstatus");
            logData.setUpdateColumnName("在校状态");
            logData.setUpdateValue(studentCompleteData.getSchoolstatus());
            logData.setOldValue(byId.getSchoolstatus());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getEducation().equals(studentCompleteData.getEducation())){
            //将修改参数存入日志
            logData.setUpdateColumn("education");
            logData.setUpdateProperty("education");
            logData.setUpdateColumnName("学历");
            logData.setUpdateValue(studentCompleteData.getEducation());
            logData.setOldValue(byId.getEducation());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getSchLength().equals(studentCompleteData.getSchLength())){
            //将修改参数存入日志
            logData.setUpdateColumn("schLength");
            logData.setUpdateProperty("sch_length");
            logData.setUpdateColumnName("学制");
            logData.setUpdateValue(studentCompleteData.getSchLength());
            logData.setOldValue(byId.getSchLength());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getIsinSchool().equals(studentCompleteData.getIsinSchool())){
            //将修改参数存入日志
            logData.setUpdateColumn("isinSchool");
            logData.setUpdateProperty("isin_school");
            logData.setUpdateColumnName("是否在校");
            logData.setUpdateValue(studentCompleteData.getIsinSchool());
            logData.setOldValue(byId.getIsinSchool());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getChangeType().equals(studentCompleteData.getChangeType())){
            //将修改参数存入日志
            logData.setUpdateColumn("changeType");
            logData.setUpdateProperty("change_type");
            logData.setUpdateColumnName("异动类别");
            logData.setUpdateValue(studentCompleteData.getChangeType());
            logData.setOldValue(byId.getChangeType());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getChangeRemarks().equals(studentCompleteData.getChangeRemarks())){
            //将修改参数存入日志
            logData.setUpdateColumn("changeRemarks");
            logData.setUpdateProperty("change_remarks");
            logData.setUpdateColumnName("异动备注");
            logData.setUpdateValue(studentCompleteData.getChangeRemarks());
            logData.setOldValue(byId.getChangeRemarks());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getFamilies().equals(studentCompleteData.getFamilies())){
            //将修改参数存入日志
            logData.setUpdateColumn("families");
            logData.setUpdateProperty("families");
            logData.setUpdateColumnName("科类");
            logData.setUpdateValue(studentCompleteData.getFamilies());
            logData.setOldValue(byId.getFamilies());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getStuType().equals(studentCompleteData.getStuType())){
            //将修改参数存入日志
            logData.setUpdateColumn("stuType");
            logData.setUpdateProperty("stu_type");
            logData.setUpdateColumnName("考生类别");
            logData.setUpdateValue(studentCompleteData.getStuType());
            logData.setOldValue(byId.getStuType());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getStudyYear().equals(studentCompleteData.getStudyYear())){
            //将修改参数存入日志
            logData.setUpdateColumn("studyYear");
            logData.setUpdateProperty("study_year");
            logData.setUpdateColumnName("学习年限");
            logData.setUpdateValue(studentCompleteData.getStudyYear());
            logData.setOldValue(byId.getStudyYear());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getNameUsedBefore().equals(studentCompleteData.getNameUsedBefore())){
            //将修改参数存入日志
            logData.setUpdateColumn("nameUsedBefore");
            logData.setUpdateProperty("nameused_before");
            logData.setUpdateColumnName("曾用名");
            logData.setUpdateValue(studentCompleteData.getNameUsedBefore());
            logData.setOldValue(byId.getNameUsedBefore());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getLevel().equals(studentCompleteData.getLevel())){
            //将修改参数存入日志
            logData.setUpdateColumn("level");
            logData.setUpdateProperty("level");
            logData.setUpdateColumnName("学历级别");
            logData.setUpdateValue(studentCompleteData.getLevel());
            logData.setOldValue(byId.getLevel());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getMajor().equals(studentCompleteData.getMajor())){
            //将修改参数存入日志
            logData.setUpdateColumn("major");
            logData.setUpdateProperty("major");
            logData.setUpdateColumnName("专业");
            logData.setUpdateValue(studentCompleteData.getMajor());
            logData.setOldValue(byId.getMajor());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getMajorCode().equals(studentCompleteData.getMajorCode())){
            //将修改参数存入日志
            logData.setUpdateColumn("majorCode");
            logData.setUpdateProperty("major_code");
            logData.setUpdateColumnName("专业代号");
            logData.setUpdateValue(studentCompleteData.getMajorCode());
            logData.setOldValue(byId.getMajorCode());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getClasses().equals(studentCompleteData.getClasses())){
            //将修改参数存入日志
            logData.setUpdateColumn("classes");
            logData.setUpdateProperty("classes");
            logData.setUpdateColumnName("班级");
            logData.setUpdateValue(studentCompleteData.getClasses());
            logData.setOldValue(byId.getClasses());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getGraduationcertificatenum().equals(studentCompleteData.getGraduationcertificatenum())){
            //将修改参数存入日志
            logData.setUpdateColumn("graduationcertificatenum");
            logData.setUpdateProperty("graduationcertificatenum");
            logData.setUpdateColumnName("毕业证编号");
            logData.setUpdateValue(studentCompleteData.getGraduationcertificatenum());
            logData.setOldValue(byId.getGraduationcertificatenum());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getNativeplace().equals(studentCompleteData.getNativeplace())){
            //将修改参数存入日志
            logData.setUpdateColumn("nativeplace");
            logData.setUpdateProperty("nativeplace");
            logData.setUpdateColumnName("籍贯");
            logData.setUpdateValue(studentCompleteData.getNativeplace());
            logData.setOldValue(byId.getNativeplace());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getSex().equals(studentCompleteData.getSex())){
            //将修改参数存入日志
            logData.setUpdateColumn("sex");
            logData.setUpdateProperty("sex");
            logData.setUpdateColumnName("性别");
            logData.setUpdateValue(studentCompleteData.getSex());
            logData.setOldValue(byId.getSex());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getNation().equals(studentCompleteData.getNation())){
            //将修改参数存入日志
            logData.setUpdateColumn("nation");
            logData.setUpdateProperty("nation");
            logData.setUpdateColumnName("民族");
            logData.setUpdateValue(studentCompleteData.getNation());
            logData.setOldValue(byId.getNation());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getAdmissionTime().equals(studentCompleteData.getAdmissionTime())){
            //将修改参数存入日志
            logData.setUpdateColumn("admissionTime");
            logData.setUpdateProperty("admission_time");
            logData.setUpdateColumnName("入学时间");
            logData.setUpdateValue(studentCompleteData.getAdmissionTime());
            logData.setOldValue(byId.getAdmissionTime());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getGraduationTime().equals(studentCompleteData.getGraduationTime())){
            //将修改参数存入日志
            logData.setUpdateColumn("graduationTime");
            logData.setUpdateProperty("graduation_time");
            logData.setUpdateColumnName("毕业时间");
            logData.setUpdateValue(studentCompleteData.getGraduationTime());
            logData.setOldValue(byId.getGraduationTime());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getExamineeNum().equals(studentCompleteData.getExamineeNum())){
            //将修改参数存入日志
            logData.setUpdateColumn("examineeNum");
            logData.setUpdateProperty("examinee_num");
            logData.setUpdateColumnName("考生号");
            logData.setUpdateValue(studentCompleteData.getExamineeNum());
            logData.setOldValue(byId.getExamineeNum());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getNormalSchStu().equals(studentCompleteData.getNormalSchStu())){
            //将修改参数存入日志
            logData.setUpdateColumn("normalSchStu");
            logData.setUpdateProperty("normal_sch_stu");
            logData.setUpdateColumnName("师范生类别");
            logData.setUpdateValue(studentCompleteData.getNormalSchStu());
            logData.setOldValue(byId.getNormalSchStu());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getDifficultStu().equals(studentCompleteData.getDifficultStu())){
            //将修改参数存入日志
            logData.setUpdateColumn("difficultStu");
            logData.setUpdateProperty("difficult_stu");
            logData.setUpdateColumnName("困难生类别");
            logData.setUpdateValue(studentCompleteData.getDifficultStu());
            logData.setOldValue(byId.getDifficultStu());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getStudentXJ().equals(studentCompleteData.getStudentXJ())){
            //将修改参数存入日志
            logData.setUpdateColumn("studentXJ");
            logData.setUpdateProperty("studentXJ");
            logData.setUpdateColumnName("学籍状态");
            logData.setUpdateValue(studentCompleteData.getStudentXJ());
            logData.setOldValue(byId.getStudentXJ());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getEntrance().equals(studentCompleteData.getEntrance())){
            //将修改参数存入日志
            logData.setUpdateColumn("entrance");
            logData.setUpdateProperty("entrance");
            logData.setUpdateColumnName("入学口");
            logData.setUpdateValue(studentCompleteData.getEntrance());
            logData.setOldValue(byId.getEntrance());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getStudentStatus().equals(studentCompleteData.getStudentStatus())){
            //将修改参数存入日志
            logData.setUpdateColumn("studentStatus");
            logData.setUpdateProperty("studentStatus");
            logData.setUpdateColumnName("学生状态");
            logData.setUpdateValue(studentCompleteData.getStudentStatus());
            logData.setOldValue(byId.getStudentStatus());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getPolitical().equals(studentCompleteData.getPolitical())){
            //将修改参数存入日志
            logData.setUpdateColumn("political");
            logData.setUpdateProperty("political");
            logData.setUpdateColumnName("政治面貌");
            logData.setUpdateValue(studentCompleteData.getPolitical());
            logData.setOldValue(byId.getPolitical());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getFamilyPhotoNumber().equals(studentCompleteData.getFamilyPhotoNumber())){
            //将修改参数存入日志
            logData.setUpdateColumn("familyPhotoNumber");
            logData.setUpdateProperty("familyPhotoNumber");
            logData.setUpdateColumnName("家庭电话");
            logData.setUpdateValue(studentCompleteData.getFamilyPhotoNumber());
            logData.setOldValue(byId.getFamilyPhotoNumber());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getStudentAddress().equals(studentCompleteData.getStudentAddress())){
            //将修改参数存入日志
            logData.setUpdateColumn("studentAddress");
            logData.setUpdateProperty("studentAddress");
            logData.setUpdateColumnName("家庭住址");
            logData.setUpdateValue(studentCompleteData.getStudentAddress());
            logData.setOldValue(byId.getStudentAddress());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getPhotoNumber().equals(studentCompleteData.getPhotoNumber())){
            //将修改参数存入日志
            logData.setUpdateColumn("photoNumber");
            logData.setUpdateProperty("photoNumber");
            logData.setUpdateColumnName("学生个人电话");
            logData.setUpdateValue(studentCompleteData.getPhotoNumber());
            logData.setOldValue(byId.getPhotoNumber());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getStudentImgUrl().equals(studentCompleteData.getStudentImgUrl())){
            //将修改参数存入日志
            logData.setUpdateColumn("studentImgUrl");
            logData.setUpdateProperty("studentImgUrl");
            logData.setUpdateColumnName("学生头像");
            logData.setUpdateValue(studentCompleteData.getStudentImgUrl());
            logData.setOldValue(byId.getStudentImgUrl());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getDormitoryInformation().equals(studentCompleteData.getDormitoryInformation())){
            //将修改参数存入日志
            logData.setUpdateColumn("dormitoryInformation");
            logData.setUpdateProperty("dormitory_information");
            logData.setUpdateColumnName("住宿具体信息");
            logData.setUpdateValue(studentCompleteData.getDormitoryInformation());
            logData.setOldValue(byId.getDormitoryInformation());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getDormitoryBuilding().equals(studentCompleteData.getDormitoryBuilding())){
            //将修改参数存入日志
            logData.setUpdateColumn("dormitoryBuilding");
            logData.setUpdateProperty("dormitory_building");
            logData.setUpdateColumnName("宿舍楼栋");
            logData.setUpdateValue(studentCompleteData.getDormitoryBuilding());
            logData.setOldValue(byId.getDormitoryBuilding());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getDormitoryUnits().equals(studentCompleteData.getDormitoryUnits())){
            //将修改参数存入日志
            logData.setUpdateColumn("dormitoryUnits");
            logData.setUpdateProperty("dormitory_units");
            logData.setUpdateColumnName("宿舍单元");
            logData.setUpdateValue(studentCompleteData.getDormitoryUnits());
            logData.setOldValue(byId.getDormitoryUnits());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getDormitoryFloor().equals(studentCompleteData.getDormitoryFloor())){
            //将修改参数存入日志
            logData.setUpdateColumn("dormitoryFloor");
            logData.setUpdateProperty("dormitory_floor");
            logData.setUpdateColumnName("宿舍楼层");
            logData.setUpdateValue(studentCompleteData.getDormitoryFloor());
            logData.setOldValue(byId.getDormitoryFloor());
            logDataMapper.addLogData(logData);
        }
        if (!byId.getDormitoryRoom().equals(studentCompleteData.getDormitoryRoom())){
            //将修改参数存入日志
            logData.setUpdateColumn("dormitoryRoom");
            logData.setUpdateProperty("dormitory_room");
            logData.setUpdateColumnName("寝室号");
            logData.setUpdateValue(studentCompleteData.getDormitoryRoom());
            logData.setOldValue(byId.getDormitoryRoom());
            logDataMapper.addLogData(logData);
        }
    }

    @Override
    public List<StudentCompleteData> findByIdGrade() {
        List<StudentCompleteData> byIdGrade = studentCompleteDataMapper.findByIdGrade();
        return byIdGrade;
    }

    @Override
    public Map findLogDataById(Integer page, Integer limit, String id) {

        Map map=new HashMap<>();
        List<LogData> logDataById = logDataMapper.findLogDataById(page, limit, id);
        int account = logDataMapper.accountLogData();
        map.put("data",logDataById);
        map.put("account",account);
        return map;
    }
}
